package com.lrh.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamMap {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,6,7,1);

        List<Integer> result = list.stream().map(i -> i * 2).collect(Collectors.toList());
        System.out.println(result);

        List<String> dishes = listDish().stream().map(d -> d.getName()).collect(Collectors.toList());
        System.out.println(dishes);

        //flatmap flat (扁平化)
        String[] words = {"Hello", "World"};

        //{H,e,l,l,o},{W,o,r,l,d}
        Stream<String[]> stream = Arrays.stream(words).map(w -> w.split("")); //Stream<String>
        //H,e,l,l,o,W,o,r,l,d
        Stream<String> stringStream = stream.flatMap(Arrays::stream);
        stringStream.distinct().forEach(System.out::println);
    }

    public static List<Dish> listDish(){
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 230, Dish.Type.OTHER),
                new Dish("rice", false, 350, Dish.Type.MEAT),
                new Dish("pizza", true, 330, Dish.Type.FISH)
        );
        return menu;
    }
}
